// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_key_ok.java

package qcs.product;

import com.jspsmart.upload.*;
import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;

public class register_key_ok extends HttpServlet
{

	public register_key_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			ServletContext context = session.getServletContext();
			String path = context.getRealPath("/");
			counter count = new counter(dbApplication);
			SmartUpload mySmartUpload = new SmartUpload();
			mySmartUpload.setCharset("UTF-8");
			nseer_db_backup1 qcs_db = new nseer_db_backup1(dbApplication);
			if (qcs_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				mySmartUpload.initialize(pageContext);
				String file_type = getFileLength.getFileType((String)session.getAttribute("unit_db_name"));
				long d = getFileLength.getFileLength((String)session.getAttribute("unit_db_name"));
				mySmartUpload.setMaxFileSize(d);
				mySmartUpload.setAllowedFilesList(file_type);
				try
				{
					mySmartUpload.upload();
					String file_name[] = new String[mySmartUpload.getFiles().getCount()];
					Date now = new Date();
					SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
					String time = formatter.format(now);
					String mod = request.getRequestURI();
					int filenum = count.readTime((String)dbSession.getAttribute("unit_db_name"), mod);
					count.writeTime((String)dbSession.getAttribute("unit_db_name"), mod);
					String qcs_id = (new StringBuilder(String.valueOf(time))).append(filenum).toString();
					mod = mod.substring(mod.lastIndexOf("/") + 1);
					String main_kind1 = mod.split("_")[0];
					String first_kind1 = mod.split("_")[1];
					String sqla = (new StringBuilder("select main_code,first_code,second_code from document_first where main_kind_name='")).append(main_kind1).append("' and first_kind_name='").append(first_kind1).append("' ").toString();
					ResultSet rsa = qcs_db.executeQuery(sqla);
					String main_code = "";
					String first_code = "";
					String second_code = "";
					if (rsa.next())
					{
						main_code = rsa.getString("main_code");
						first_code = rsa.getString("first_code");
						second_code = rsa.getString("second_code");
					}
					qcs_id = (new StringBuilder(String.valueOf(main_code))).append(first_code).append(second_code).append(qcs_id).toString();
					for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
					{
						SmartFile file = mySmartUpload.getFiles().getFile(i);
						if (file.isMissing())
						{
							file_name[i] = "";
						} else
						{
							int filenum1 = count.read((String)dbSession.getAttribute("unit_db_name"), "qcsAttachmentcount");
							count.write((String)dbSession.getAttribute("unit_db_name"), "qcsAttachmentcount", filenum1);
							file_name[i] = (new StringBuilder(String.valueOf(filenum1))).append(file.getFileName()).toString();
							file.saveAs((new StringBuilder(String.valueOf(path))).append("qcs/file_attachments/").append(filenum1).append(file.getFileName()).toString());
						}
					}

					String product_id = mySmartUpload.getRequest().getParameter("product_id");
					String product_name = mySmartUpload.getRequest().getParameter("product_name");
					String purchase_qcs_way = mySmartUpload.getRequest().getParameter("purchase_qcs_way");
					String intrmanu_qcs_way = mySmartUpload.getRequest().getParameter("intrmanu_qcs_way");
					String crm_deliver_qcs_way = mySmartUpload.getRequest().getParameter("crm_deliver_qcs_way");
					String crm_return_qcs_way = mySmartUpload.getRequest().getParameter("crm_return_qcs_way");
					String manu_product_qcs_way = mySmartUpload.getRequest().getParameter("manu_product_qcs_way");
					String manu_procedure_qcs_way = mySmartUpload.getRequest().getParameter("manu_procedure_qcs_way");
					String stock_qcs_way = mySmartUpload.getRequest().getParameter("stock_qcs_way");
					String other_qcs_way = mySmartUpload.getRequest().getParameter("other_qcs_way");
					String register = mySmartUpload.getRequest().getParameter("register");
					String register_id = mySmartUpload.getRequest().getParameter("register_id");
					String register_time = mySmartUpload.getRequest().getParameter("register_time");
					String bodyab = new String(mySmartUpload.getRequest().getParameter("remark").getBytes("UTF-8"), "UTF-8");
					String remark = exchange.toHtml(bodyab);
					sqla = "insert into qcs_product_config(qcs_id,product_id,product_name,purchase_qcs_way,intrmanu_qcs_way,crm_deliver_qcs_way,crm_return_qcs_way,manu_product_qcs_way,manu_procedure_qcs_way,stock_qcs_way,other_qcs_way,register_id,register,register_time,remark";
					String sqlb = (new StringBuilder(") values ('")).append(qcs_id).append("','").append(product_id).append("','").append(product_name).append("','").append(purchase_qcs_way).append("','").append(intrmanu_qcs_way).append("','").append(crm_deliver_qcs_way).append("','").append(crm_return_qcs_way).append("','").append(manu_product_qcs_way).append("','").append(manu_procedure_qcs_way).append("','").append(stock_qcs_way).append("','").append(other_qcs_way).append("','").append(register_id).append("','").append(register).append("','").append(register_time).append("','").append(remark).append("'").toString();
					for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
					{
						int p = i + 1;
						sqla = (new StringBuilder(String.valueOf(sqla))).append(",attachment").append(p).toString();
						sqlb = (new StringBuilder(String.valueOf(sqlb))).append(",'").append(file_name[i]).append("'").toString();
					}

					String sql = (new StringBuilder(String.valueOf(sqla))).append(sqlb).append(")").toString();
					qcs_db.executeUpdate(sql);
					sql = (new StringBuilder("update design_file set qcs_tag='1' where product_id='")).append(product_id).append("'").toString();
					qcs_db.executeUpdate(sql);
					List rsList = GetWorkflow.getList(qcs_db, "qcs_config_workflow", "05");
					if (rsList.size() == 0)
					{
						sql = (new StringBuilder("update qcs_product_config set check_tag='1' where qcs_id='")).append(qcs_id).append("'").toString();
						qcs_db.executeUpdate(sql);
						sql = (new StringBuilder("update design_file set purchase_qcs_way='")).append(purchase_qcs_way).append("',intrmanu_qcs_way='").append(intrmanu_qcs_way).append("',crm_deliver_qcs_way='").append(crm_deliver_qcs_way).append("',crm_return_qcs_way='").append(crm_return_qcs_way).append("',manu_product_qcs_way='").append(manu_product_qcs_way).append("',manu_procedure_qcs_way='").append(manu_procedure_qcs_way).append("',stock_qcs_way='").append(stock_qcs_way).append("',other_qcs_way='").append(other_qcs_way).append("' where product_id='").append(product_id).append("'").toString();
						qcs_db.executeUpdate(sql);
					} else
					{
						for (Iterator ite = rsList.iterator(); ite.hasNext(); qcs_db.executeUpdate(sql))
						{
							String elem[] = (String[])ite.next();
							sql = (new StringBuilder("insert into qcs_workflow(config_id,object_ID,describe1,describe2) values ('")).append(elem[0]).append("','").append(qcs_id).append("','").append(elem[1]).append("','").append(elem[2]).append("')").toString();
						}

					}
					response.sendRedirect("qcs/product/register_key_ok.jsp?finished_tag=0");
					qcs_db.commit();
					qcs_db.close();
				}
				catch (Exception ex)
				{
					response.sendRedirect("qcs/product/register_key_ok.jsp?finished_tag=1");
				}
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
